package com.shop.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.shop.entity.PageResult;
import com.shop.entity.Result;
import com.shop.mapper.ProductMapper;
import com.shop.pojo.Product;
import com.shop.pojo.ProductExample;
import com.shop.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.UUID;

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired private ProductMapper productMapper;

    @Override
    /* 查询所有上架商品，并分页显示*/
    public PageResult findAllSellProduct(Product product, int pageNum, int pageSize) {

        return this.findProduct(product, pageNum, pageSize, 1);
    }

    @Override
    /* 查询所有下架商品，并分页显示*/
    public PageResult findAllNoSellProduct(Product product, int pageNum, int pageSize) {

        return this.findProduct(product, pageNum, pageSize, 0);
    }

    /** 抽取查找商品的代码 */
    public PageResult findProduct(Product product, int pageNum, int pageSize, Integer is_sell) {
        PageHelper.startPage(pageNum, pageSize);
        ProductExample tbe = null;
        if (product != null) {
            tbe = new ProductExample();
            ProductExample.Criteria criteria = tbe.createCriteria();

            if (product.getIsSell() != null) {
                criteria.andIsSellEqualTo(is_sell);
            }
        }
        Page<Product> page = (Page<Product>) productMapper.selectByExample(tbe);

        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    /*添加商品*/
    public Result addProduct(Product product) {
        Result result = null;
        if (StringUtils.isEmpty(product.getName())
                || StringUtils.isEmpty(product.getImage())
                || product.getShopPrice() == null
                || product.getMarketPrice() == null) {
            result = new Result(false, "请完善商品信息");
            return result;
        }
        product.setId(UUID.randomUUID().toString());
        product.setDate(new Date());
        product.setIsSell(1);
        product.setIsHot(0);
        product.setIsDelete(1);
        productMapper.insertSelective(product);
        result = new Result(true, "添加成功", product);
        return result;
    }

    @Override
    /*删除商品*/
    public Result deleteProduct(String id) {
        Result result = null;
        if (StringUtils.isEmpty(id)) {
            result = new Result(false, "参数错误");
            return result;
        }
        Product product = productMapper.selectByPrimaryKey(id);
        if (product != null) {
            product.setIsDelete(0);
            productMapper.updateByPrimaryKeySelective(product);
            result = new Result(true, "删除成功", product);
        } else {
            result = new Result(false, "查无此商品");
        }

        return result;
    }

    @Override
    /*修改商品*/
    public Result updateProduct(Product product) {

        Result result = null;
        if (StringUtils.isEmpty(product.getName())
                || StringUtils.isEmpty(product.getImage())
                || product.getShopPrice() == null
                || product.getMarketPrice() == null) {

            result = new Result(false, "请完善商品信息");
            return result;
        }

        productMapper.updateByPrimaryKeySelective(product);
        result = new Result(true, "修改成功", product);
        return result;
    }

    @Override
    /*查找单个商品*/
    public Product findProductById(String pid) {
        Product product = productMapper.selectByPrimaryKey(pid);
        if (product != null) {
            return product;
        }
        return null;
    }

    @Override
    /*模糊查找商品（根据名称模糊查找）*/
    public PageResult fuzzySearchProduct(Product product, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        ProductExample tbe = null;
        if (product != null) {
            tbe = new ProductExample();
            ProductExample.Criteria criteria = tbe.createCriteria();

            if (product.getIsSell() != null) {
                criteria.andNameLike("%" + product.getName() + "%");
            }
        }
        Page<Product> page = (Page<Product>) productMapper.selectByExample(tbe);

        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    /*商品上架*/
    public Result putAwayProduct(Product product) {
        Result result = null;
        if (product == null) {
            result = new Result(false, "商品信息错误");
            return result;
        }
        product.setIsSell(1);
        productMapper.updateByPrimaryKeySelective(product);
        result = new Result(true, "上架成功", product);
        return result;
    }

    @Override
    /*商品下架*/
    public Result soldOutProduct(Product product) {
        Result result = null;
        if (product == null) {
            result = new Result(false, "商品信息错误");
            return result;
        }
        product.setIsSell(0);
        productMapper.updateByPrimaryKeySelective(product);
        result = new Result(true, "下架成功", product);
        return result;
    }
}
